<?php
namespace app\cms\controller;
use app\common\base\_Db;
use app\common\entity\RequestEntity;
use Endroid\QrCode\QrCode;
use qrc\Aes;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/3
 * Time: 9:27
 */
class Line extends Controller{
    /*
     * 线路
     * 数据列表
     */
    public function ListItem(){
        $request_data = [
            $get_keyword = new RequestEntity('search_key', '搜索内容', false, 0)
        ];
        _Db::Validates($request_data);
        $where = '1=1';
        if(!empty($get_keyword->text)){
            if (is_numeric($get_keyword->text)){
                $where .= ' and d.price = ' .$get_keyword->text;
            }else{
                $where .= ' and a.line_from  like "%' .$get_keyword->text.'%" or a.line_destination like "%' . $get_keyword->text .'%"' ;
            }
        }
        $param_page = input('Page', 1);
        $param_pagesize = 10;
        $result = Db::table('line')
            ->field(
                [
                    'a.line_id'           => 'Id',          //主键id
                    'a.line_from'         => 'From',        //起点
                    'a.line_destination'  => 'Destination', //终点
                    'a.oper_date'         => 'Date',        //操作日期
                    'a.price'             => 'Price',       //票价
                    'c.nick_name'           => 'OperName',  //操作人
                ]
            )
            ->alias('a')
            ->join('staff c','a.oper_id = c.staff_id','LEFT')
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->order('a.oper_date desc')
            ->select();
        foreach ( $result as $key => $value){
            $result[$key]['Order'] = $key + 1;
        };
        $int_total_count = count(Db::table('line')
            ->alias('a')
            ->join('staff c','a.oper_id = c.staff_id','LEFT')
            ->where($where)
            ->order('a.oper_date desc')
            ->select());
        $data['tableData']=$result;
        $data['TotalCount'] = $int_total_count;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        _Db::ResponseJson($data);
    }
    /*
     * 详细信息
     */
    public function ItemInfo(){
        //获取请求参数
        $request_data = [
            $itemid = new RequestEntity('ItemId', 'Id', true, 0)
        ];
        _Db::Validates($request_data);
        $param_where = [
            'a.line_id' => $itemid->text,
        ];
        $result = Db::table('line')
            ->field(
                [
                    'a.line_id'           => 'Id',          //主键id
                    'a.line_from'         => 'From',        //起点
                    'a.line_destination'  => 'Destination', //终点
                    'a.oper_date'         => 'Date',        //操作日期
                    'a.price'             => 'Price',       //票价
                    'c.nick_name'           => 'OperName',  //操作人
                ]
            )
            ->alias('a')
            ->join('staff c','a.oper_id = c.staff_id','LEFT')
            ->where($param_where)
            ->find();
        return json_encode($result,true);
    }
    /*
     * 司机二维码
     */
    public function DriverQrc(){
        //获取请求参数
        $request_data = [
            $itemid = new RequestEntity('ItemId', 'Id', true, 0),
            $price = new RequestEntity('Price', 'Price', true, 0),
        ];
        _Db::Validates($request_data);
        //司机二维码
        $res=array(
            'cardTime'  => time(),
            'price'     => (int)$price->text,
            'line'      => (int)$itemid->text, //判断参数
            'type'      => 'driver'  //订单流水号
        );
        $res = json_encode($res);
        $aes = new Aes('1234567812345678');
        //加密
        $encrypted = $aes->encrypt($res);
        $qrCode = new QrCode();
        ob_start();
        $qrCode ->setText($encrypted);
        //$qrCode  ->setSize(300);//大小
        $qrCode ->setLabelFontPath(VENDOR_PATH.'endroid/qrcode/assets/fonts/noto_sans.otf');
        $qrCode ->setErrorCorrectionLevel('high');
        $qrCode ->setLabelFontSize(16);
        header('Content-Type: '.$qrCode->getContentType());
        $png = $qrCode->writeString();
        $imageString = base64_encode($png);
        ob_end_clean();
        $Img =  'data:image/png;base64,'.$imageString;
        $result = _Db::GetData('line','line_from,line_destination,price','line_id = '.$itemid->text);
        $result['Img'] = $Img;
        $data['qrcData']=$result;
        return json_encode($data);
        //return '<img src="data:image/png;base64,'.$imageString.'">';
    }
    /*
     * 修改数据
     */
    public function ModifyItem(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 0),
            $post_from = new RequestEntity('From', '起点', true, 30),
            $post_destination = new RequestEntity('Destination', '终点', true, 50),
            $post_price = new RequestEntity('Price','票价', false, 0),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        //验证表单内容
        _Db::Validates($request_data);
        $res = array(
            'line_from'         => $post_from->text,           //起点
            'line_destination'  => $post_destination->text,    //终点
            'price'             => $post_price->text,          //票价
            'oper_id'           => $post_creatid->text,        //操作人
            'oper_date'         => date('Y-m-d H:i:s',time()), //操作日期
        );
        $param_where = [
            'line_id' => $post_itemid->text,
        ];
        $result = Db::table('line')
            ->where($param_where)
            ->update($res);
        if ($result){
            _Db::log($post_creatid->text,3,'线路修改');
            $this->Item($param_where);
        }

    }
    /*
     * 保存数据
     */
    public function AddItem(){
        //获取请求参数
        $request_data = [
            $post_from = new RequestEntity('From', '起点', true, 30),
            $post_destination = new RequestEntity('Destination', '终点', true, 50),
            $post_price = new RequestEntity('Price','票价', false, 0),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        //验证表单内容
        _Db::Validates($request_data);
        $param_field = array(
            'line_from'         => $post_from->text,           //起点
            'line_destination'  => $post_destination->text,    //终点
            'price'             => $post_price->text,          //票价
            'oper_id'           => $post_creatid->text,        //操作人
            'oper_date'         => date('Y-m-d H:i:s',time()), //操作日期
        );
        $res = Db::table('line')->insert($param_field);
        if ($res){
            _Db::log($post_creatid->text,1,'线路添加');
            $this->ListItem();
        }
    }
    /*
     * 删除表数据
     */
    public function RemoveItem(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 0),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30)
        ];
        //表单内容验证
        _Db::Validates($request_data);
        $param_where = [
            'line_id' => $post_itemid->text,
        ];
        $result = Db::table('line')
            ->where($param_where)
            ->delete();
        if ($result){
            _Db::log($post_creatid->text,2,'线路删除');
            $this->ListItem();
        }
    }
    //是否投放
    public function SwitchModity(){
        //获取请求参数
        $request_data = [
            $post_itemid = new RequestEntity('ItemId', 'ID', true, 10),
            $post_value = new RequestEntity('Value', '值', true, 10),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        //表单内容验证
        //_Db::Validates($request_data);
        $param_field = '';
        if ($post_value->text == 1){
            $type = 6;
            $content = '线路撤销';
            $param_field = [
                'line_status'    => 1,
                'oper_id'               => $post_creatid->text,
                'oper_date' => date("Y-m-d H:i:s",time()),
            ];
        }elseif ($post_value->text == 2){
            $type = 5;
            $content = '线路投放';
            $param_field = [
                'line_status'    => 2,
                'oper_id'               => $post_creatid->text,
                'oper_date' => date("Y-m-d H:i:s",time()),
            ];
        }
        //设置条件
        $param_where = [
            'line_id' => $post_itemid->text,
        ];
        $result = Db::table('line')
            ->where($param_where)
            ->update($param_field);
        if ($result){
            _Db::log($post_creatid->text,$type,$content);
            $this->Item($param_where);
        }
    }
    public function Item($param_where){
        $result = Db::table('line')
            ->field(
                [
                    'a.line_id'           => 'Id',          //主键id
                    'a.line_from'         => 'From',        //起点
                    'a.line_destination'  => 'Destination', //终点
                    'a.oper_date'         => 'Date',        //操作日期
                    'a.price'             => 'Price',       //票价
                    'c.nick_name'         => 'OperName',     //操作人
                ]
            )
            ->alias('a')
            ->join('staff c','a.oper_id = c.staff_id','LEFT')
            ->where($param_where)
            ->order('a.oper_date desc')
            ->find();
        _Db::ResponseJson($result);
    }
}